package com.oscar.ehome.view.pageview.adapter;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.pagebean.StudyListBean;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.database.DataBaseDAO;
import com.oscar.ehome.utils.dowload.db.DbDataOperation;
import com.oscar.ehome.utils.dowload.db.ThreadDAOImpl;
import com.oscar.ehome.utils.dowload.dowload.DownloadManager;
import com.oscar.ehome.utils.dowload.dowload.DownloadTask;
import com.oscar.ehome.utils.dowload.entity.ThreadInfo;
import com.oscar.ehome.utils.dowload.progressbar.RoundProgressBar;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/14
 * 下载列表
 */

public class DownloadAdapter extends BaseAdapter {

    private ArrayList<StudyListBean> list;
    private LayoutInflater mInflater;
    private Context mContext = null;
    OnClickedsListener onClickedsListener;

    public DownloadAdapter(Context context, ArrayList<StudyListBean> list) {
        this.mContext = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    public ArrayList<StudyListBean> getmData() {
        return list;
    }

    public void setmData(ArrayList<StudyListBean> smData) {
        this.list = smData;
    }


    public interface OnClickedsListener {
        public void onClicks(int postion, int download, RoundProgressBar progressBar);
    }

    public void setOnClicksListener(OnClickedsListener onClickedsListener) {
        this.onClickedsListener = onClickedsListener;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_downlist, null);
            holder = new ViewHolder();
            holder.downlist_title = (TextView) convertView.findViewById(R.id.downlist_title);
            holder.downlist_size = (TextView) convertView.findViewById(R.id.downlist_size);
            holder.downlist_img = (ImageView) convertView.findViewById(R.id.downlist_img);
            holder.downlist_downimg = (ImageView) convertView.findViewById(R.id.downlist_downimg);
            holder.downProgressBar = (RoundProgressBar) convertView.findViewById(R.id.downProgressBar);
            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }

        //判断正在下载的文件 更新下载进度比
        final StudyListBean bean = list.get(position);
        File f = new File(bean.getVideo_Path());
        String name = bean.getTitle();
        int state = bean.getVideoState();

        holder.downlist_title.setText(name);

        String importurl = C.qiniu_ip + bean.getSmallImg();
//        GildeUtils.imgLoadPic(mContext, importurl, holder.downlist_img, "net");
        GildeUtils.loadUrlImages(importurl, holder.downlist_img, mContext);

        switch (state) {
            case 0://未下载
                holder.downlist_downimg.setImageResource(R.mipmap.xiazailiebiao);
                TintManager.setTint(holder.downlist_downimg, Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                holder.downProgressBar.setVisibility(View.GONE);
                holder.downlist_downimg.setVisibility(View.VISIBLE);
                break;
            case 1://正在下载
                // 开始执行
//                    int finish = bookPeogrees.get(list.get(position).getId());
//                    holder.downProgressBar.setVisibility(View.VISIBLE);
//                    holder.downlist_downimg.setVisibility(View.GONE);
//                    holder.downProgressBar.setProgress(finish);
//                    DownloadManager.instance(mContext).addObservable(list.get(position).getDownloadurl(), obs);
//                    ToastUtil.e("viviv", "bookPeogrees.get(mData.get(position).getId():" + bookPeogrees.get(list.get(position).getId()));
//                    if (bookPeogrees != null && bookPeogrees.get(list.get(position).getId()) == 100) {//已下载
//                        bean.setVideoState(3);
//                        DbDataOperation.updateDownloadState(resolver, 3, bean.getMediaFile());//1表示正在下载
//                        holder.downProgressBar.setVisibility(View.GONE);
//                        holder.downlist_downimg.setVisibility(View.VISIBLE);
//                        holder.downlist_downimg.setImageResource(R.mipmap.duigou);
//                        DownloadManager.instance(mContext).addObservable(list.get(position).getDownloadurl(), obs);
//                        String sizes = String.valueOf(CommonUtils.getFileOrFilesSize(bean.getVideo_Path(), 3));
//                        holder.downlist_size.setText(sizes);
//                    }
                break;
            case 3://
                holder.downProgressBar.setVisibility(View.GONE);
                holder.downlist_downimg.setVisibility(View.VISIBLE);
                holder.downlist_downimg.setImageResource(R.mipmap.duigou);
                TintManager.setTint(holder.downlist_downimg, Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                String sizes = CommonUtils.getFileOrFilesSize(bean.getVideo_Path(), 3);
                holder.downlist_size.setText(sizes);
                break;
            case 2://暂停状态
                holder.downProgressBar.setVisibility(View.GONE);
                holder.downlist_downimg.setVisibility(View.VISIBLE);
                holder.downlist_downimg.setImageResource(R.mipmap.zanting);
                TintManager.setTint(holder.downlist_downimg, Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                break;
            default:
                break;
        }
        //暂停时
        holder.downProgressBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getVideoState() == 1) {//下载时
                    holder.downProgressBar.setVisibility(View.GONE);
                    holder.downlist_downimg.setVisibility(View.VISIBLE);
                    onClickedsListener.onClicks(position, 3, holder.downProgressBar);//暂停
                }
            }
        });
        //下载时
        holder.downlist_downimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getVideoState() == 0) {//未开始下载时
                    holder.downProgressBar.setVisibility(View.VISIBLE);
                    holder.downlist_downimg.setVisibility(View.GONE);
                    onClickedsListener.onClicks(position, 1, holder.downProgressBar);//开始下载
                } else if (bean.getVideoState() == 2) {//暂停时
                    holder.downProgressBar.setVisibility(View.VISIBLE);
                    holder.downlist_downimg.setVisibility(View.GONE);
                    onClickedsListener.onClicks(position, 2, holder.downProgressBar);//继续下载
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView downlist_title;//标题
        ImageView downlist_img;//图片
        TextView downlist_size;//大小
        ImageView downlist_downimg;
        RoundProgressBar downProgressBar;
    }
}
